<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


/*
 * create or update user information 
 * 
 * @param args = {
 * 
 *  @param id
 *  @param name
 *  @param email
 *  @param msisdn
 *  @param country
 *  @param date
 *  @param status
 * 
 * } 
 */
function users_save($args){

    //import required global variables 
    global $db;
    global $contex;
    
    //extract and sanitize parameters creating a variable for each
    $fields = X_param_array ($args, 'fields'); 
    $id = X_param ($args, 'id'); 
    $data['name'] = X_param ($args, 'name'); if(!$id && !$data['name']) return __MOOV_PARAM_INVAL_;
    $data['email'] = X_param ($args, 'email'); 
    $data['msisdn'] = X_param ($args, 'msisdn'); 
    $data['country'] = X_param ($args, 'country'); 
    $data['date'] = X_param ($args, 'date', gmdate('y-m-d h:m:s')); 
    $data['status'] = X_param ($args, 'status'); 
    $sort = X_param_array ($args, 'sort');
    if(!$data['msisdn'] && !$data['email']) return __MOOV_PARAM_INVAL_;
    
     // ---------------- Operation begins here ---------------------
    $id = $db->save('users',$id,$data);
    return users_list(array('id' => $id, 'fields' => $fields, 'limit' => $limit, 'sort' => $sort));    
   
}

/*
 * List one or more users on the platform
 * 
 * @param args = {
 * 
 *  @param fields
 *  @param id
 *  @param name
 *  @param email
 *  @param msisdn
 *  @param country
 *  @param date
 *  @param status
 * 
 * }  
 */
function users_list($args){
   
    //import required global variables 
    global $db;
    global $contex;
    global $user;

    //extract and sanitize parameters creating a variable for each
    $fields = X_param_array ($args, 'fields'); 
    $data['id'] = X_param_array ($args, 'id'); 
    $data['name'] = X_param_array ($args, 'name'); 
    $data['email'] = X_param_array ($args, 'email'); 
    $data['msisdn'] = X_param_array ($args, 'msisdn'); 
    $data['country'] = X_param_array ($args, 'country'); 
    $data['date'] = X_param_array ($args, 'date'); 
    $data['status'] = X_param_array ($args, 'status'); 
    $limit = X_param_array ($args, 'limit');
    $sort = X_param_array ($args, 'sort');
    
    if($contex['user_id'] && !$data['id'])  $data['id'] = $contex['user_id'];
    // ---------------- Operation begins here ---------------------
    $rc = $db->get('users', $fields, $data, $limit, $sort);
    return out($rc);
}


/*
 * Verify users on the platform by creating a token and validating them against 
 * that token
 * 
 * @param args = {
 * 
 *  @param fields
 *  @param id
 *  @param user_id
 *  @param code
 *  @param msisdn
 *  @param email
 * 
 * }  
 */
function users_verification($args){
   
    //import required global variables 
    global $db;
    global $contex;
    global $user;

    //extract and sanitize parameters creating a variable for each
    $fields = X_param_array ($args, 'fields'); 
    $data['id'] = X_param ($args, 'id'); 
    $data['user_id'] = X_param ($args, 'user_id'); if(!$data['user_id']) return __MOOV_PARAM_INVAL_;
    $data['code'] = X_param ($args, 'code');
    
    if($contex['user_id'] && !$data['id'])  $data['id'] = $contex['user_id'];
    // ---------------- Operation begins here ---------------------
    $rs = users_token_list(array('fields' => 'user_id:id', 'user_id' => $data['user_id'], 'code' => $data['code']));
    if(!$rs[0]) $rs[0]['status'] = 'F';
    else $rs[0]['status'] = 'T';

    return out($rs);
}



/*
 * Verify users on the platform by creating a token and validating them against 
 * that token
 * 
 * @param args = {
 * 
 *  @param fields
 *  @param id
 *  @param user_id
 *  @param code
 *  @param msisdn
 *  @param email
 * 
 * }  
 */

function users_token_send($args){
   
    //import required global variables 
    global $db;
    global $contex;
    global $user;

    //extract and sanitize parameters creating a variable for each
    $fields = X_param_array ($args, 'fields'); 
    $data['user_id'] = X_param ($args, 'user_id'); 
    $data['msisdn'] = X_param ($args, 'msisdn');
    $data['email'] = X_param ($args, 'email');
    if(!$data['email'] && !$data['msisdn'] && !$data['user_id']) return __MOOV_PARAM_INVAL_;
    
    if($contex['user_id'] && !$data['id'])  $data['id'] = $contex['user_id'];
    // ---------------- Operation begins here ---------------------
    $rs = users_token_save(array('fields' => 'code', 'user_id' => $data['user_id']));
    if(!$rs[0]) $rs[0]['status'] = 'F';
    else $rs[0]['status'] = 'T';
    
//    //moov on to send sms or mail
//    if($data['email']){
//        use '\google\appengine\api\mail\Message';
//
//        try
//        {
//          $message = new Message();
//          $message->setSender("tobimartins4uall@google.com");
//          $message->addTo($data['email']);
//          $message->setSubject("Moov - user verification");
//          $message->setTextBody($rs[0]['code']);
//          $message->send();
//        } catch (InvalidArgumentException $e) {
//            
//        }
//    }else if($data['msisdn']){
//        //send sms
//    }
    return out($rs);
}

/*
 * create or update user information 
 * 
 * @param args = {
 * 
 *  @param fields
 *  @param id
 *  @param user_id
 *  @param token
 * 
 * } 
 */
function users_token_save($args){

    //import required global variables 
    global $db;
    global $contex;
    
    //extract and sanitize parameters creating a variable for each
    $fields = X_param_array ($args, 'fields'); 
    $id = X_param ($args, 'id'); 
    $data['user_id'] = X_param ($args, 'user_id'); if(!$id && !$data['user_id']) return __MOOV_PARAM_INVAL_;
    $sort = X_param_array ($args, 'sort');
    $data['code'] = mk_random_token(); 
    
     // ---------------- Operation begins here ---------------------
    //create or update token
    if(!$id)$rs = users_token_list(array('fields' => 'id,user_id', 'user_id' => $data['user_id'] ,'limit' => $limit, 'sort' => $sort));
    if($rs[0]['id']) $id = $db->save('users_token',$rs[0]['id'],$data);
    else  $id = $db->save('users_token',$id,$data);
    
    return users_token_list(array('id' => $id, 'fields' => $fields, 'limit' => $limit, 'sort' => $sort));    
  
}

/*
 * List one or more users tokens on the platform
 * 
 * @param args = {
 * 
 *  @param fields
 *  @param id
 *  @param user_id
 *  @param token
 * 
 * } 
 */
function users_token_list($args){
   
    //import required global variables 
    global $db;
    global $contex;
    global $user;

    //extract and sanitize parameters creating a variable for each
    $fields = X_param_array ($args, 'fields'); 
    $data['id'] = X_param_array ($args, 'id'); 
    $data['user_id'] = X_param_array ($args, 'user_id'); 
    $data['code'] = X_param_array ($args, 'code');  
    $limit = X_param_array ($args, 'limit');
    $sort = X_param_array ($args, 'sort');
    
    if($contex['user_id'] && !$data['id'])  $data['id'] = $contex['user_id'];
    // ---------------- Operation begins here ---------------------
    $rc = $db->get('users_token', $fields, $data, $limit, $sort);
    return out($rc);
}
